package com.ddw.meedeeuh.views;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import com.ddw.meedeeuh.presenters.MovieImportPresenter.MovieImportView;

@SuppressWarnings("serial")
public class MovieImportViewImpl extends JDialog implements MovieImportView {

	private final JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
	private final JLabel statusLabel = new JLabel();
	private final JButton buttonCancel = new JButton("Cancel");
	private final JButton buttonOk = new JButton("OK");

	public MovieImportViewImpl(Component owner) {
		super((Frame)SwingUtilities.getRoot(owner));

		setTitle("Movie Import");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

		final JPanel panel = (JPanel) getContentPane();
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.setLayout(new GridBagLayout());
		panel.setPreferredSize(new Dimension(360, 100));

		addStatusLabel(panel);
		addCancelButton(panel);
		addOkButton(panel);
		addProgressBar(panel);
		pack();
	}

	private void addProgressBar(final JPanel panel) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.insets = new Insets(0, 0, 5, 0);
		gbc.ipadx = gbc.ipady = 10;

		panel.add(this.progressBar, gbc);
	}

	private void addCancelButton(final JPanel panel) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.EAST;
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.insets = new Insets(5, 0, 0, 0);
		panel.add(this.buttonCancel, gbc);
	}

	private void addOkButton(final JPanel panel) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.EAST;
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.insets = new Insets(5, 0, 0, 0);
		panel.add(this.buttonOk, gbc);
	}

	private void addStatusLabel(final JPanel panel) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		gbc.gridx = gbc.gridy = 0;
		gbc.insets = new Insets(0, 0, 5, 0);
		panel.add(this.statusLabel, gbc);
	}

	@Override
	public Component asComponent() {
		return this;
	}

	@Override
	public void showView() {
		setLocationRelativeTo(getOwner());
		setVisible(true);
	}

	@Override
	public void hideView() {
		setVisible(false);
	}

	@Override
	public void setProgress(final int percentage) {
		// Check we are in the right thread
		if(SwingUtilities.isEventDispatchThread() == false) {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					setProgress(percentage);
				}
			});
			return;
		}

		this.progressBar.setValue(percentage);
		this.progressBar.validate();
	}

	@Override
	public void setMessage(final String message) {
		// Check we are in the right thread
		if(SwingUtilities.isEventDispatchThread() == false) {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					setMessage(message);
				}
			});
			return;
		}

		this.statusLabel.setText(message);
		this.statusLabel.validate();
	}

	@Override
	public void addCloseListener(final ActionListener listener) {
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				listener.actionPerformed(new ActionEvent(MovieImportViewImpl.this, ActionEvent.ACTION_PERFORMED, null));
			}

		});
		this.buttonCancel.addActionListener(listener);
	}

	@Override
	public boolean validateClose() {
		return JOptionPane.showConfirmDialog(this,
				"Do you wish to cancel movie importing?\nAny changes made so far will be saved.",
				"Import", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
	}

	@Override
	public void addOkListener(ActionListener listener) {
		this.buttonOk.addActionListener(listener);
	}

	@Override
	public void setOkVisible(boolean visible) {
		this.buttonOk.setVisible(visible);
	}

	@Override
	public void setCancelVisible(boolean visible) {
		this.buttonCancel.setVisible(visible);
	}

}
